import 'package:bookkeeping/db/db_helper.dart';
import 'package:bookkeeping/login/register.dart';
import 'package:bookkeeping/login/userInfo.dart';
import 'package:bookkeeping/main/main_page.dart';
import 'package:bookkeeping/widgets/LoginTextInput.dart';
import 'package:bookkeeping/widgets/custom_button.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';


class Login extends StatefulWidget {

  Login({Key key}) : super(key: key);

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {

  static TextEditingController _accountController = new TextEditingController();
  static TextEditingController _passwordController = new TextEditingController();

  @override
  void initState() {
     // TODO: implement initState
     super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: true,
      body: Container(
        width: double.maxFinite,
        height: double.maxFinite,
        padding: EdgeInsets.only(left: 50, right: 50),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Image.asset('assets/images/icons/note.jpg'),
            SizedBox(height: 40),
            LoginTextInput(
              hintText: '请输入账号',
              textEditingController: _accountController,
            ),
            SizedBox(height: 20),
            LoginTextInput(
              hintText: '请输入密码',
              textEditingController: _passwordController,
            ),
            SizedBox(height: 60),
            Padding(
              padding: EdgeInsets.only(left: 20, right: 20),
              child: CustomButton(
                color: Colors.teal,
                content: '登录',
                onTap: (){
                  _login();
                },
              )
            ),
            SizedBox(height: 20),
            GestureDetector(
              onTap: (){
                Navigator.push<dynamic>(
                  context,
                  MaterialPageRoute<dynamic>(
                    builder: (BuildContext context) => Register()
                  ),
                );
              },
              child: Text('没有账号? 去注册 >'),
            )
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  _login(){
    if(_accountController.text == ''){
      showToast('账号不可为空');
    }else if(_passwordController.text == ''){
      showToast('密码不可为空');
    }else {
      _sqliteCheck();
    }
  }

  _sqliteCheck() async {
    UserInfo userInfo = await dbHelp.getUserInfoByAccount(_accountController.text);
    print('userInfo: $userInfo');
    if(userInfo == null){

      showToast('该用户不存在！');

    } else {

      if(userInfo.password == _passwordController.text){

        showToast('登录成功');

        Navigator.push<dynamic>(
          context,
          MaterialPageRoute<dynamic>(
              builder: (BuildContext context) => MainPage()
          ),
        );

        // SharedPreferencesHelper.save('account', _accountController.text);
        // SharedPreferencesHelper.save('password', _passwordController.text);
        //
        // String account = await SharedPreferencesHelper.get('account');
        // String password = await SharedPreferencesHelper.get('password');
        //
        // print('account: $account');
        // print('password: $password');

      } else {
        showToast('密码输入错误');
      }

    }
  }
}
